package com.szh.cloud.api.user.service.Impl;

import com.szh.cloud.api.user.entity.ApiSearchUserEntity;
import com.szh.cloud.api.user.repositoy.ApiUserDao;
import com.szh.cloud.api.user.service.ApiUserService;
import com.szh.cloud.mybatis.eltree.ElTreeItem;
import com.szh.cloud.mybatis.eltree.ElTreeResult;
import com.szh.cloud.mybatis.entity.PtMenuEntity;
import com.szh.cloud.mybatis.entity.PtUserEntity;
import com.szh.cloud.mybatis.flexigrid.PagingBean;
import com.szh.cloud.user.entity.UserEntity;
import com.szh.cloud.user.execption.RepeatEmailException;
import com.szh.cloud.user.execption.RepeatMobileException;
import com.szh.cloud.user.execption.RepeatUserNameException;
import com.szh.cloud.user.execption.UseNameCcupdException;
import com.szh.cloud.user.service.UserService;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class ApiUserServiceImpl implements ApiUserService {
    @Resource
    private ApiUserDao apiUserDao;
    @Resource
    private UserService userService;
    @Resource
    private MessageSource messageSource;
    @Override
    public List<PtUserEntity> queryUserListByPage(ApiSearchUserEntity entity, PagingBean paging) {
        paging.deal(PtUserEntity.class);
        List<PtUserEntity> list = apiUserDao.queryUserListByPage(entity,paging);
        return list;
    }

    @Override
    public Integer queryUserListTotal(ApiSearchUserEntity entity) {
        Integer total = apiUserDao.queryUserListTotal(entity);
        return total;
    }

    @Override
    public void addUser(UserEntity entity) throws RepeatEmailException, RepeatUserNameException, RepeatMobileException {
        userService.addUserInfo(entity);
    }

    @Override
    public void editUser(UserEntity userEntity) throws RepeatEmailException, RepeatUserNameException, RepeatMobileException {
        userService.modifyUserInfo(userEntity);
    }

    @Override
    public void deleteUserRecycleById(String userUuids) throws UseNameCcupdException {
        userService.deleteUserRecycleById(userUuids);
    }

    @Override
    public ElTreeResult queryRoleTreeByUser(String userUuid) {
        ElTreeResult elTreeResult =  new ElTreeResult();

        Locale locale = LocaleContextHolder.getLocale();
        String managerName = messageSource.getMessage("user.role.allocate",null,locale);
        List<ElTreeItem> rootList = new ArrayList<>();
        ElTreeItem rootItem = new ElTreeItem();
        rootItem.setId("a1");
        rootItem.setLabel(managerName);
        List<ElTreeItem> list = apiUserDao.queryRoleTree();
        rootItem.setChildren(list);
        rootList.add(rootItem);
        elTreeResult.setDateList(rootList);

        List<String> checkedList = apiUserDao.queryRoleTreeById(userUuid);
        elTreeResult.setCheckedList(checkedList);

        return elTreeResult;
    }

    @Override
    public void saveRoleByUser(String userUuids, String roleIds) {
        userService.saveRoleByUser(userUuids,roleIds);
    }
}